package com.zhou;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * T5
 *
 * @author zhouxianfeng
 * @date 2021-8-17
 */
public class T5 {

    private static Node root = new Node();

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] s = in.nextLine().split(" ");
        int libNum = Integer.parseInt(s[0]);
        int problemNum = Integer.parseInt(s[1]);
        List<String> lib = new ArrayList<>();
        for (int i = 0; i < libNum; i++) {
            addNode(root, in.nextLine(), 0);
        }
        List<String> problems = new ArrayList<>();
        for (int i = 0; i < problemNum; i++) {
            problems.add(Query(root, in.nextLine(), 0));
        }
        problems.forEach(System.out::println);

    }

    public static void addNode(Node node, String num, int index) {
        if (index < num.length()) {
            node.count += 1;
            char c = num.charAt(index);
            boolean flag = false;
            for (Node next : node.list) {
                if (next.curr == c) {
                    addNode(next, num, ++index);
                    flag = true;
                }
            }
            if (!flag) {
                Node next = new Node(c, 0);
                node.list.add(next);
                addNode(next, num, ++index);
            }
        }
    }

    public static String Query(Node node, String num, int index) {
        String result = null;
        if (index < num.length()) {
            char c = num.charAt(index);
            for (Node next : node.list) {
                if (next.curr == c) {
                    result = Query(next, num, ++index);
                    if (result == null) {
                        result = index + " " + next.count;
                        return result;
                    } else {
                        return result;
                    }

                }
            }
            if (index == 0 && result == null) {
                return index + " " + node.count;
            }
        }
        return result;

    }


    public static class Node {
        Character curr;
        int count;
        List<Node> list = new ArrayList<>();

        Node(char curr, int count) {
            this.curr = curr;
            this.count = count;
        }

        Node() {

        }
    }
}
